package com.apache.passport.service.impl;

import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.passport.entity.BlackRoster;
import com.apache.passport.manager.BlackRosterManager;

/**
 * description:黑名单操作  
 * @author Hou Dayu 创建时间：2014-12-18  
 * @Copyright(c)2014:
 */
public class BlackRosterManagerImpl implements BlackRosterManager {

	protected IDao blackRosterDao;
	protected final String entityName = "com.apache.passport.entity.BlackRoster";
	protected Map<String, PluginConnector> plugins;

	public void setPlugins(Map<String, PluginConnector> plugins) {
		this.plugins = plugins;
	}

	public void setBlackRosterDao(IDao blackRosterDao) {
		this.blackRosterDao = blackRosterDao;
	}

	public String saveInfo(ParamsVo<BlackRoster> vo) throws BusinessException {
		BlackRoster blackRoster = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="blackRoster_"+infoId;
		blackRoster.setBrId(infoId);
		MethodParam param = new MethodParam("BlackRoster", cacheKey, "", entityName);
		param.setVaule(blackRoster);
		if (blackRosterDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BlackRoster> vo) throws BusinessException {
		BlackRoster blackRoster = vo.getObj();
		if (Validator.isNotNull(blackRoster.getBrId())) {
			String cacheKey = "";
			//String cacheKey="blackRoster_"+blackRoster.getBrId();
			MethodParam param = new MethodParam("BlackRoster", cacheKey, "", entityName);
			param.setVaule(blackRoster);
			return blackRosterDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BlackRoster> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="blackRoster_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BlackRoster info = (BlackRoster) blackRosterDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BlackRoster");
			return blackRosterDao.edit(param);
		} else {
			param.setParams("brId", infoId);
			param.setDelete(true);
			return blackRosterDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BlackRoster> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="blackRoster_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return blackRosterDao.selectById(param);
	}

	public Object execute(ParamsVo<BlackRoster> vo) {
		String key = vo.getMethodKey();
		try {
			if (!Validator.isEmpty(plugins)) {
				if (!Validator.isEmpty(plugins.get(key))) {
					return plugins.get(key).execute(vo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;
	}

	public Page getPageInfo(ParamsVo<BlackRoster> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = blackRosterDao.pageSelect(param);
		return page;
	}

	public List<BlackRoster> getList(ParamsVo<BlackRoster> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return blackRosterDao.select(param);
	}

	public long countInfo(ParamsVo<BlackRoster> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return blackRosterDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BlackRoster> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(blackRosterDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BlackRoster blackRoster = vo.getObj();
		if (Validator.isNotNull(blackRoster.getBrAccount())) {
			sb.append(" and brAccount = :brAccount");
			param.setParams("brAccount", blackRoster.getBrAccount());
		}
		if (Validator.isNotNull(blackRoster.getBrIpaddress())) {
			sb.append(" and brIpaddress = :brIpaddress");
			param.setParams("brIpaddress", blackRoster.getBrIpaddress());
		}
		if (Validator.isNotNull(blackRoster.getBrRemark())) {
			sb.append(" and brRemark = :brRemark");
			param.setParams("brRemark", blackRoster.getBrRemark());
		}
		if (Validator.isNotNull(blackRoster.getBrCreatetime())) {
			sb.append(" and brCreatetime = :brCreatetime");
			param.setParams("brCreatetime", blackRoster.getBrCreatetime());
		}
		if (Validator.isNotNull(blackRoster.getBrRelievetime())) {
			sb.append(" and brRelievetime = :brRelievetime");
			param.setParams("brRelievetime", blackRoster.getBrRelievetime());
		}
		if (Validator.isNotNull(blackRoster.getUserEname())) {
			sb.append(" and userEname = :userEname");
			param.setParams("userEname", blackRoster.getUserEname());
		}
		if (Validator.isNotNull(blackRoster.getBrRelieveremark())) {
			sb.append(" and brRelieveremark = :brRelieveremark");
			param.setParams("brRelieveremark", blackRoster.getBrRelieveremark());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
